import {Component} from '@angular/core';

import {AlertController, NavController, NavParams, ToastController, ViewController} from 'ionic-angular';
import {UserData} from "../../../common/user.data";
import {Storage} from "@ionic/storage";
import {EnshrineService} from "../../../service/article/enshrine.service";
import {RegisterPage} from "../../register/register";
import {LoginPage} from "../../login/login";

@Component({
  selector: 'page-article-operator',
  templateUrl: 'operator.html'
})
export class ScheduleFilterPage {
  private article: any;

  constructor(public viewCtrl: ViewController,
              private storage: Storage,
              private user: UserData,
              private enshrineService: EnshrineService,
              private navController: NavController,
              private alertCtrl: AlertController,
              private toastCtrl: ToastController,
              private  navParams: NavParams) {
    this.article = this.navParams.get('article');
  }

  collect() {
    this.storage.get("oauthToken").then(value => {
      if (!value) {
        this.alertCtrl.create({
          title: '注册登录后继续操作',
          buttons: [{
            text: '注册',
            handler: () => {
              this.navController.push(RegisterPage);
            }
          }, {
            text: '登录',
            handler: () => {
              this.navController.push(LoginPage);
            }
          }]
        }).present();
      } else {
        this.enshrineService.enshrine({articleCode: this.article.code, token: value}).subscribe(data => {
          this.toastCtrl.create({
            message: data.message,
            duration: 2000
          }).present();
        }, () => {
          this.toastCtrl.create({
            message: '收藏失败！',
            duration: 2000
          }).present();
        });
        this.viewCtrl.dismiss();
      }
    });
  }

  share() {
  }

  whistleBlowing() {

  }
}
